﻿using System;
using TracedReality.SpatialTrees;
using QiHe.Yaml.Grammar;

namespace TracedReality
{
    /// <summary>
    /// This is so horrible but it is what I am using for now.
    /// </summary>
    public class Settings
    {
        public static readonly double EPSILON = 0.0000001;

        public static int numBounces = 1;
        public static int numShadowSamples = 1;
        public static int numAntialiasSamples = 1;
        public static int width = 400;
        public static int height = 400;
        public static Vector backColor = new Vector(0, 0, 0);
        public static Vector ambientColor = new Vector(0, 0, 0);
        public static double minContribution = 0.00196;
        public static double airIndex = 1.000277;

        public static bool parallelRender = true;
        public static int chunks = (int)Math.Ceiling(Math.Sqrt(Environment.ProcessorCount));

        public static bool useSDSCaster = false;
        public static string SDSType = "KD";
        public static int maxSDSNodeCount = 25;
        public static int maxSDSDepth = 10;

        public static bool usePhotonMapping = false;
        public static int numPhotonBounces = 10;
        public static int numPhotons = 100000;
        public static int maxPhotonsPerNode = 10;
        public static int maxPhotonTreeDepth = 20;
        public static int numPhotonsToCollect = 100;
        public static int maxGatherIterations = 10;
        public static double intialGatherRadius = 1 / 3.0;
        public static double iterationRadiusIncrease = 1 / 3.0;
        public static double coneFilterK = 1;

        public static void Parse(MappingEntry entry)
        {
            if (entry == null) { throw new ArgumentNullException("entry"); }

            Mapping settings = (Mapping)entry.Value;
            foreach (MappingEntry e in settings.Entries)
            {
                switch (e.Key.ToString())
                {
                    case "numBounces":
                        numBounces = int.Parse(e.Value.ToString());
                        break;
                    case "numShadowSamples":
                        numShadowSamples = int.Parse(e.Value.ToString());
                        break;
                    case "numAntialiasSamples":
                        numAntialiasSamples = int.Parse(e.Value.ToString());
                        break;
                    case "width":
                        width = int.Parse(e.Value.ToString());
                        break;
                    case "height":
                        height = int.Parse(e.Value.ToString());
                        break;
                    case "backColor":
                        backColor = Vector.FromSequence((Sequence)e.Value);
                        break;
                    case "ambientColor":
                        ambientColor = Vector.FromSequence((Sequence)e.Value);
                        break;
                    case "useSDS":
                        useSDSCaster = bool.Parse(e.Value.ToString());
                        break;
                    case "SDSType":
                        SDSType = e.Value.ToString();
                        break;
                }
            }
        }
    }
}
